<?php
    abstract class MappingObject
    {
        private $prop = [];
        abstract public function save($conn);
        abstract protected function insert($conn);
        abstract protected function update($conn);
        protected function set_prop($key,$value = NULL)
        {
             $this->prop[strtolower($key)] = $value;
        }
        protected function get_prop($key)
        {
             return $this->prop[strtolower($key)];
        }
        public function __set($name,$value)
        {
            $name = strtolower($name);
            if (array_key_exists($name,$this->prop))
            {
                $this->prop[$name] = $value;
                return $value;
            }
        }
        public function __get($name)
        {
            $name = strtolower($name);
            if (array_key_exists($name,$this->prop))
            {
                return $this->prop[$name] ;
            }
        }
        public function __isset($name)
        {
            $name = strtolower($name);
            return isset($this->prop[$name]);
        }
        /**  As of PHP 5.1.0  */
        public function __unset($name)
        {
            $name = strtolower($name);
            unset($this->prop[$name]);
        }
        abstract protected function exist();
    }    
    class Hotel extends MappingObject
    {
        
        public function __construct()
        {
            $this->set_prop("wc_uuid");
            $this->set_prop("web_url");
            $this->set_prop("policy");
            $this->set_prop("checkin_instructions");
            $this->set_prop("room_fees");
            $this->set_prop("entity_id");
            $this->set_prop("customer_id");
            $this->set_prop("hotel_id");
            $this->set_prop("hotel_wcid");
            $this->set_prop("service");
            $this->set_prop("rating");
            $this->set_prop("amenities");
            $this->set_prop("cert_level");
            $this->set_prop("ean_id");
            $this->set_prop("name");
            $this->set_prop("city_id");
            $this->set_prop("latitude");
            $this->set_prop("longitude");
            $this->set_prop("address");
            $this->set_prop("image_head");
            $this->set_prop("image_normal_set");
            $this->set_prop("image_small_set");
            $this->set_prop("category",1);
            $this->set_prop("sub_category","HOTEL");
            $this->set_prop("click_count",0);
            $this->set_prop("description");
            $this->set_prop("lang","en");
            $this->set_prop("area_info");
            $this->set_prop("room_info");
            $this->set_prop("floor_count");
            $this->set_prop("room_count");
            $this->set_prop("check_in");
            $this->set_prop("check_out");
            $this->set_prop("exist",FALSE);
        }
        protected function exist()
        {
            return $this->exist;
        }
        public function save($conn)
        {
            if ($this->exist())
            {
                $this->update($conn);
            }
            else
            {
                $this->insert($conn);
            }
            return [$this->customer_id,$this->entity_id,$this->hotel_id,$this->city_id];
        }
        protected function insert($conn)
        {
            $sql = "INSERT INTO Customers (CustomerID,CertLevel,CustomerName_{$this->lang}) ";
            $sql .= " VALUES (:id,:cert,:name) ";
            $stmt = $conn->prepare($sql);
            $stmt->bindValue(":id",$this->customer_id,PDO::PARAM_INT);
            $stmt->bindValue(":cert",$this->cert_level,PDO::PARAM_INT);
            $stmt->bindValue(":name",$this->name,PDO::PARAM_STR);
            $stmt->execute();
            $sql = "INSERT INTO CustomerEntity (EntityID,CustomerID,CityID,EntityLatitude,EntityLongitude,EntityAddress,EntityImageHead,EntityImageSetNormal,EntityImageSetSmall,TypeCategory,TypeSubcategory,CNTClick,HomePage) ";
            $sql .= " VALUES ( ";
            $sql .= " :eId ,  ";
            $sql .= " :cusid ,  ";
            $sql .= " :cid ,  ";
            $sql .= " :elat ,  ";
            $sql .= " :elong ,  ";
            $sql .= " :eaddress ,  ";
            $sql .= " :eimage_he ,  ";
            $sql .= " :eimage_normal ,  ";
            $sql .= " :eimage_small ,  ";
            $sql .= " :etype_category ,  ";
            $sql .= " :esubcategory ,  ";
            $sql .= " :eclick ,  ";
            $sql .= " :web_url ";
            $sql .= " ) ";
            $stmt = $conn->prepare($sql);
            $stmt->bindValue(":eId",$this->entity_id,PDO::PARAM_INT);
            $stmt->bindValue(":cusid",$this->customer_id ,PDO::PARAM_INT);
            $stmt->bindValue(":cid",$this->city_id,PDO::PARAM_STR);
            $stmt->bindValue(":elat",$this->latitude,PDO::PARAM_INT);
            $stmt->bindValue(":elong",$this->longitude ,PDO::PARAM_INT);
            $stmt->bindValue(":eaddress",$this->address,PDO::PARAM_STR);
            $stmt->bindValue(":eimage_he",$this->image_head,PDO::PARAM_STR);
            $stmt->bindValue(":eimage_normal",$this->image_normal_set ,PDO::PARAM_STR);
            $stmt->bindValue(":eimage_small",$this->image_small_set,PDO::PARAM_STR);
            $stmt->bindValue(":etype_category",$this->category,PDO::PARAM_INT);
            $stmt->bindValue(":esubcategory",$this->sub_category,PDO::PARAM_STR);
            $stmt->bindValue(":eclick",$this->click_count,PDO::PARAM_INT);
            $stmt->bindValue(":web_url",$this->web_url,PDO::PARAM_STR);
            $stmt->execute();
            $sql = " INSERT INTO Entity_{$this->lang} ";
            $sql .= " ( EntityID , EntityName, EntityDescription ) ";
            $sql .= " VALUES (";
            $sql .= " :eId , :eName, :eDesc ) ";
            $stmt = $conn->prepare($sql);
            $stmt->bindValue(":eId",$this->entity_id,PDO::PARAM_INT);
            $stmt->bindValue(":eName",$this->name ,PDO::PARAM_STR);
            $stmt->bindValue(":eDesc",$this->description,PDO::PARAM_STR);
            $stmt->execute();
            $sql = " INSERT INTO Hotels";
            $sql .= " ( HotelID , EntityID , HotelService, HotelRating, HotelWCUUID , HotelEANID , NumberOfRooms , NumberOfFloors , CheckIN, CheckOut ) ";
            $sql .= " VALUES (";
            $sql .= " :hID , :eId, :hser , :hrat , :huuid , :hean_id , :room_count , :f_count, :cin , :cout ) ";
            $stmt = $conn->prepare($sql);
            $stmt->bindValue(":hID",$this->hotel_id,PDO::PARAM_INT);
            $stmt->bindValue(":eId",$this->entity_id ,PDO::PARAM_INT);
            $stmt->bindValue(":hser",$this->service,PDO::PARAM_INT);
            $stmt->bindValue(":hrat",$this->rating,PDO::PARAM_INT);
            $stmt->bindValue(":huuid",$this->wc_uuid ,PDO::PARAM_STR);
            $stmt->bindValue(":hean_id",$this->ean_id,PDO::PARAM_INT);
            $stmt->bindValue(":room_count",$this->room_count,PDO::PARAM_INT);
            $stmt->bindValue(":f_count",$this->floor_count,PDO::PARAM_INT);
            $stmt->bindValue(":cin",$this->check_in ,PDO::PARAM_STR);
            $stmt->bindValue(":cout",$this->check_out ,PDO::PARAM_STR);
            $stmt->execute();
            $sql = " INSERT INTO Hotels_{$this->lang}";
            $sql .= " ( HotelID , HotelWCID , HotelAmenities , HotelAreaInfo , HotelRoomInfo , HotelPolicy , HotelCheckInInstructions , FeesDesc) ";
            $sql .= " VALUES (";
            $sql .= " :hID , :hwc, :hf , :harea , :hroom , :h_policy , :h_instruct , :h_feesdesc ) ";
            $stmt = $conn->prepare($sql);
            $stmt->bindValue(":hID",$this->hotel_id,PDO::PARAM_INT);
            $stmt->bindValue(":hwc",$this->hotel_wcid ,PDO::PARAM_INT);
            $stmt->bindValue(":hf",$this->amenities,PDO::PARAM_STR);
            $stmt->bindValue(":harea",$this->area_info,PDO::PARAM_STR);
            $stmt->bindValue(":hroom",$this->room_info,PDO::PARAM_STR);
            $stmt->bindValue(":h_policy",$this->policy,PDO::PARAM_STR);
            $stmt->bindValue(":h_instruct",$this->checkin_instructions,PDO::PARAM_STR);
            $stmt->bindValue(":h_feesdesc",$this->room_fees,PDO::PARAM_STR);
            $stmt->execute();
        }
        protected function update($conn)
        {
            $sql = " INSERT INTO Entity_{$this->lang} ";
            $sql .= " ( EntityID , EntityName, EntityDescription ) ";
            $sql .= " VALUES (";
            $sql .= " :eId , :eName, :eDesc ) ";
            $stmt = $conn->prepare($sql);
            $stmt->bindValue(":eId",$this->entity_id,PDO::PARAM_INT);
            $stmt->bindValue(":eName",$this->name ,PDO::PARAM_STR);
            $stmt->bindValue(":eDesc",$this->description,PDO::PARAM_STR);
            $stmt->execute();
            $sql = " INSERT INTO Hotels_{$this->lang}";
            $sql .= " ( HotelID , HotelWCID , HotelAmenities , HotelAreaInfo , HotelRoomInfo , HotelPolicy , HotelCheckInInstructions , FeesDesc) ";
            $sql .= " VALUES (";
            $sql .= " :hID , :hwc, :hf  , :harea , :hroom , :h_policy , :h_instruct , :h_feesdesc) ";
            $stmt = $conn->prepare($sql);
            $stmt->bindValue(":hID",$this->hotel_id,PDO::PARAM_INT);
            $stmt->bindValue(":hwc",$this->hotel_wcid ,PDO::PARAM_INT);
            $stmt->bindValue(":hf",$this->amenities,PDO::PARAM_STR);
            $stmt->bindValue(":harea",$this->area_info,PDO::PARAM_STR);
            $stmt->bindValue(":hroom",$this->room_info,PDO::PARAM_STR);
            $stmt->bindValue(":h_policy",$this->policy,PDO::PARAM_STR);
            $stmt->bindValue(":h_instruct",$this->checkin_instructions,PDO::PARAM_STR);
            $stmt->bindValue(":h_feesdesc",$this->room_fees,PDO::PARAM_STR);
            $stmt->execute();
            $sql = "UPDATE Customers SET CustomerName_{$this->lang} = :name WHERE CustomerID = :id ";
            $stmt = $conn->prepare($sql);
            $stmt->bindValue(":name",$this->name,PDO::PARAM_STR);
            $stmt->bindValue(":id",$this->customer_id ,PDO::PARAM_INT);
            $stmt->execute();
            
        }
    }
?>